<template>
  <div>
    <div>{{ $t("message.TrussDesign.elementSelect.face.select") }}</div>
    <query-list
        v-model="params.curveFace.elements"
        :active="queryListActiveMap.sectionCurveFace"
        @switchQuery="switchQuery('sectionCurveFace')"
        :tipMsg="params.curveFace.message"
    />
    <div>{{ $t("message.TrussDesign.elementSelect.startPoint.select") }}</div>
    <query-list
        v-model="params.startPoint.elements"
        :active="queryListActiveMap.sectionSPoints"
        @switchQuery="switchQuery('sectionSPoints')"
        :tipMsg="params.startPoint.message"
    />
    <div>{{ $t("message.TrussDesign.elementSelect.endPoint.select") }}</div>
    <query-list
        v-model="params.endPoint.elements"
        :active="queryListActiveMap.sectionEPoints"
        @switchQuery="switchQuery('sectionEPoints')"
        :tipMsg="params.endPoint.message"
    />
    <div>{{ $t("message.TrussDesign.elementSelect.curve.select") }}</div>
    <query-list
        v-model="params.path.elements"
        :active="queryListActiveMap.sectionLine"
        @switchQuery="switchQuery('sectionLine')"
        @removeItem="removeHighLight"
        :tipMsg="params.path.message"
    />
  </div>
</template>
<script>
import {eventBus} from "../../../../eventbus/eventbus";
import {ElementType, GeomType} from "../../../../modeling/TypeDefine";
import {PluginCADToolFunctionType} from "../../../../../nodeApp/static/lib/crowncad-plugin-sdk-0.0.1";
import trussDesignCommand from "../TrussDesignCommand";

export default {
  name: "FaceElementSelect",
  data() {
    return {
      params: {
        curveFace: {
          elements: [],
          message: this.$t("message.TrussDesign.elementSelect.face.message")
        },
        startPoint: {
          elements: [],
          message: this.$t("message.TrussDesign.elementSelect.startPoint.message")
        },
        endPoint: {
          elements: [],
          message: this.$t("message.TrussDesign.elementSelect.endPoint.message")
        },
        path: {
          elements: [],
          allElementIds: [],
          message: this.$t("message.TrussDesign.elementSelect.curve.message")
        }
      },
      queryListActiveMap: {
        sectionCurveFace: 1,
        sectionSPoints: 0,
        sectionEPoints: 0,
        sectionLine: 0
      },
      activeQueryName: "sectionCurveFace",
    }
  },
  mounted() {
    this.cbId = eventBus.on('selectElement', (e) => {
      if (!e || !e.element || !e.element.length) {
        return
      }
      let item = e.element[0].element;
      switch (this.activeQueryName) {
        case "sectionCurveFace":
          if (item.type !== ElementType.FACE) {
            trussDesignCommand.eventBusAlertMsg(this.$t("message.TrussDesign.elementSelect.face.tip"), "warning")
            break;
          }
          if (item.params.surfaceType !== GeomType.CYLINDER
              && item.params.surfaceType !== GeomType.TABCYLINDER
              && item.params.surfaceType !== GeomType.NURBSSURFACE
              && item.params.surfaceType !== GeomType.CONE
              && item.params.surfaceType !== GeomType.RULE) {
            trussDesignCommand.eventBusAlertMsg(this.$t("message.TrussDesign.elementSelect.face.tip"), "warning")
            break;
          }
          this.params.curveFace.elements = [];
          this.params.curveFace.elements.push(item)
          break;
        case "sectionSPoints":
          if (item.type !== ElementType.POINTONCURVE || !item?.id?.includes("_")) {
            trussDesignCommand.eventBusAlertMsg(this.$t("message.TrussDesign.elementSelect.startPoint.tip"), "warning")
            break;
          }
          this.params.startPoint.elements = [];
          this.params.startPoint.elements.push(item);
          break;
        case "sectionEPoints":
          if (item.type !== ElementType.POINTONCURVE || !item?.id?.includes("_")) {
            trussDesignCommand.eventBusAlertMsg(this.$t("message.TrussDesign.elementSelect.endPoint.tip"), "warning")
            break;
          }
          this.params.endPoint.elements = [];
          this.params.endPoint.elements.push(item)
          break;
        case "sectionLine":
          if (item.type !== ElementType.CURVE) {
            trussDesignCommand.eventBusAlertMsg(this.$t("message.TrussDesign.elementSelect.curve.tip"), "warning")
            break;
          }
          if (this.params.path.elements.find(j => j.id === item.id)) {
            break;
          }
          this.params.path.elements.push(item)
          this.params.path.allElementIds = [...this.params.path.elements.map(d => d.id)];
          trussDesignCommand.toolQuery.highLightEntityOutline(this.params.path.allElementIds);
          break;
      }
    });
  },
  methods: {
    //激活的选取框
    switchQuery(name) {
      this.activeQueryName = name;
      for (const key in this.queryListActiveMap) {
        if (key === name) {
          this.queryListActiveMap[key] = 1;
        } else {
          this.queryListActiveMap[key] = 0;
        }
      }
    },
    // 移出线条段的高亮
    removeHighLight(item) {
      trussDesignCommand.toolQuery.clearEntityOutline(item?.id)
      this.params.path.elements = this.params.path.elements.filter(ele => ele.id !== item.id)
      this.params.path.allElementIds = [...this.params.path.elements.map(d => d.id)];
    }
  },
  destroyed() {
    eventBus.off("selectElement")
  }
}
</script>
<style scoped>

</style>